﻿<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.IO;
using System.Net;
using System.Web;

public class UploadHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
        string myData = "";//返回的参数（文件名）
        try {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            string strUploadPath = context.Request.PhysicalApplicationPath + context.Request.QueryString["folder"].Trim('/').Replace("/", "\\") + "\\";
            for (int i = 0; i < context.Request.Files.Count; i++) {
                HttpPostedFile postedFile = context.Request.Files[i];
                //重命名文件
                Random ra = new Random();
                DateTime d1 = DateTime.Now;
                String picName = d1.ToString("yyyyMMddHHmmss") + ra.Next(100, 999).ToString();
                string extension = Path.GetFileName(postedFile.FileName).Substring(Path.GetFileName(postedFile.FileName).LastIndexOf("."));
                
                string fileName = strUploadPath + picName + extension;
                myData = picName + extension;
                if (fileName != "") {
                    postedFile.SaveAs(fileName);
                }
            }
            context.Response.Write(myData);//返回值
        }
        catch (Exception ex) {
            context.Response.ContentType = "text/plain";
            context.Response.Write(ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}